package com.cody.controller.comment;

import com.cody.entity.Comment;
import com.cody.entity.Student;
import com.cody.entity.Teacher;
import com.cody.service.CommentService;
import com.cody.service.StudentService;
import com.cody.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Cody
 * @date 2021/4/9 - 21:18
 */
@Controller
public class CommentController{
    @Autowired
    private CommentService commentService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/comment/insert")
    public String insertComment(@RequestParam("questionId")Integer questionId , @RequestParam("parentCommentId")String parentCommentId,@RequestParam("reply")String content, HttpSession session){
        String userType = (String) session.getAttribute("userType");
        if("学生".equals(userType)){
            Student student = (Student) session.getAttribute("user");
            student = studentService.querySingleStudentByStudentNumber(student.getStudentNumber());
            String reviewId = student.getStudentNumber();
            commentService.insertComment(new Comment(1,reviewId,student.getStudentName(),new Date(),content,Integer.parseInt(parentCommentId),questionId,0));
            return "redirect:/student/index";

        }
         if("老师".equals(userType)){
            Teacher teacher = (Teacher) session.getAttribute("user");
             //teacher = teacherService.querySingleTeacherByTeacherNumber(teacher.getTeacherNumber());
            String reviewId = teacher.getTeacherNumber();
             teacher = teacherService.querySingleTeacherByTeacherNumber(reviewId);
             commentService.insertComment(new Comment(2,reviewId,teacher.getTeacherName(),new Date(),content,Integer.parseInt(parentCommentId),questionId,1));
            return "redirect:/teacher/index";
        }
        return "/login";
    }
    @RequestMapping("/comment/del")
    @ResponseBody
    public Map<String, String> del(@RequestParam(required = false, name = "commentId") int commentId) {
        Map<String, String> map = new HashMap<>();
        boolean flag = commentService.delCommentByCommentId(commentId);
        if(flag){
            map.put("msg","删除成功");
        }
        return map;
    }
    @PostMapping("/comment/update")
    public String update(@RequestParam(required = false, name = "commentId") String commentId,
                         @RequestParam(required = false, name = "commentContent") String commentContent,
                         HttpSession session) {

        Comment comment = commentService.queryCommentByCommentId(Integer.parseInt(commentId));
        comment.setCommentContent(commentContent);
        commentService.updCommentInfo(comment);
        return "redirect:/admin/index";
    }

    @RequestMapping("/comment/{commentId}")
    public void querySingleComment(@PathVariable("commentId") int commentId,
                         HttpSession session) {
        Comment comment = commentService.queryCommentByCommentId(commentId);
        session.setAttribute("c",comment);
    }
}
